home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / jpsrc2.zip / JCMCU.C < prev    next >
C/C++ Source or Header  |  1991-10-03  |  6KB  |  213 lines

  1. /*
  2.  * jcmcu.c
  3.  *
  4.  * Copyright (C) 1991, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains MCU extraction routines and quantization scaling.
  9.  * These routines are invoked via the extract_MCUs and
  10.  * extract_init/term methods.
  11.  */
  12.  
  13. #include "jinclude.h"
  14.  
  15.  
  16. /*
  17.  * If this file is compiled with -DDCT_ERR_STATS, it will reverse-DCT each
  18.  * block and sum the total errors across the whole picture.  This provides
  19.  * a convenient method of using real picture data to test the roundoff error
  20.  * of a DCT algorithm.  DCT_ERR_STATS should *not* be defined for a production
  21.  * compression program, since compression is much slower with it defined.
  22.  * Also note that jrevdct.o must be linked into the compressor when this
  23.  * switch is defined.
  24.  */
  25.  
  26. #ifdef DCT_ERR_STATS
  27. static int dcterrorsum;        /* these hold the error statistics */
  28. static int dcterrormax;
  29. static int dctcoefcount;    /* This will probably overflow on a 16-bit-int machine */
  30. #endif
  31.  
  32.  
  33. /* ZAG[i] is the natural-order position of the i'th element of zigzag order. */
  34.  
  35. static const short ZAG[DCTSIZE2] = {
  36.   0,  1,  8, 16,  9,  2,  3, 10,
  37.  17, 24, 32, 25, 18, 11,  4,  5,
  38.  12, 19, 26, 33, 40, 48, 41, 34,
  39.  27, 20, 13,  6,  7, 14, 21, 28,
  40.  35, 42, 49, 56, 57, 50, 43, 36,
  41.  29, 22, 15, 23, 30, 37, 44, 51,
  42.  58, 59, 52, 45, 38, 31, 39, 46,
  43.  53, 60, 61, 54, 47, 55, 62, 63
  44. };
  45.  
  46.  
  47. LOCAL void
  48. extract_block (JSAMPARRAY input_data, int start_row, long start_col,
  49.            JBLOCK output_data, QUANT_TBL_PTR quanttbl)
  50. /* Extract one 8x8 block from the specified location in the sample array; */
  51. /* perform forward DCT, quantization scaling, and zigzag reordering on it. */
  52. {
  53.   /* This routine is heavily used, so it's worth coding it tightly. */
  54.   DCTBLOCK block;
  55. #ifdef DCT_ERR_STATS
  56.   DCTBLOCK svblock;        /* saves input data for comparison */
  57. #endif
  58.  
  59.   { register JSAMPROW elemptr;
  60.     register DCTELEM *localblkptr = block;
  61. #if DCTSIZE != 8
  62.     register short elemc;
  63. #endif
  64.     register short elemr;
  65.  
  66.     for (elemr = DCTSIZE; elemr > 0; elemr--) {
  67.       elemptr = input_data[start_row++] + start_col;
  68. #if DCTSIZE == 8        /* unroll the inner loop */
  69.       *localblkptr++ = (DCTELEM) GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  70.       *localblkptr++ = (DCTELEM) GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  71.       *localblkptr++ = (DCTELEM) GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  72.       *localblkptr++ = (DCTELEM) GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  73.       *localblkptr++ = (DCTELEM) GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  74.       *localblkptr++ = (DCTELEM) GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  75.       *localblkptr++ = (DCTELEM) GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  76.       *localblkptr++ = (DCTELEM) GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  77. #else
  78.       for (elemc = DCTSIZE; elemc > 0; elemc--) {
  79.     *localblkptr++ = (DCTELEM) GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  80.       }
  81. #endif
  82.     }
  83.   }
  84.  
  85. #ifdef DCT_ERR_STATS
  86.   memcpy((void *) svblock, (void *) block, SIZEOF(DCTBLOCK));
  87. #endif
  88.  
  89.   j_fwd_dct(block);
  90.  
  91.   { register JCOEF temp;
  92.     register short i;
  93.  
  94.     for (i = 0; i < DCTSIZE2; i++) {
  95.       temp = (JCOEF) block[ZAG[i]];
  96.       /* divide by *quanttbl, ensuring proper rounding */
  97.       if (temp < 0) {
  98.     temp = -temp;
  99.     temp += *quanttbl>>1;
  100.     temp /= *quanttbl;
  101.     temp = -temp;
  102.       } else {
  103.     temp += *quanttbl>>1;
  104.     temp /= *quanttbl;
  105.       }
  106.       *output_data++ = temp;
  107.       quanttbl++;
  108.     }
  109.   }
  110.  
  111. #ifdef DCT_ERR_STATS
  112.   j_rev_dct(block);
  113.  
  114.   { register int diff;
  115.     register short i;
  116.  
  117.     for (i = 0; i < DCTSIZE2; i++) {
  118.       diff = block[i] - svblock[i];
  119.       if (diff < 0) diff = -diff;
  120.       dcterrorsum += diff;
  121.       if (dcterrormax < diff) dcterrormax = diff;
  122.     }
  123.     dctcoefcount += DCTSIZE2;
  124.   }
  125. #endif
  126. }
  127.  
  128.  
  129. /*
  130.  * Extract samples in MCU order, process & hand off to output_method.
  131.  * The input is always exactly N MCU rows worth of data.
  132.  */
  133.  
  134. METHODDEF void
  135. extract_MCUs (compress_info_ptr cinfo,
  136.           JSAMPIMAGE image_data,
  137.           int num_mcu_rows,
  138.           MCU_output_method_ptr output_method)
  139. {
  140.   JBLOCK MCU_data[MAX_BLOCKS_IN_MCU];
  141.   int mcurow;
  142.   long mcuindex;
  143.   short blkn, ci, xpos, ypos;
  144.   jpeg_component_info * compptr;
  145.   QUANT_TBL_PTR quant_ptr;
  146.  
  147.   for (mcurow = 0; mcurow < num_mcu_rows; mcurow++) {
  148.     for (mcuindex = 0; mcuindex < cinfo->MCUs_per_row; mcuindex++) {
  149.       /* Extract data from the image array, DCT it, and quantize it */
  150.       blkn = 0;
  151.       for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  152.     compptr = cinfo->cur_comp_info[ci];
  153.     quant_ptr = cinfo->quant_tbl_ptrs[compptr->quant_tbl_no];
  154.     for (ypos = 0; ypos < compptr->MCU_height; ypos++) {
  155.       for (xpos = 0; xpos < compptr->MCU_width; xpos++) {
  156.         extract_block(image_data[ci],
  157.               (mcurow * compptr->MCU_height + ypos)*DCTSIZE,
  158.               (mcuindex * compptr->MCU_width + xpos)*DCTSIZE,
  159.               MCU_data[blkn], quant_ptr);
  160.         blkn++;
  161.       }
  162.     }
  163.       }
  164.       /* Send the MCU whereever the pipeline controller wants it to go */
  165.       (*output_method) (cinfo, MCU_data);
  166.     }
  167.   }
  168. }
  169.  
  170.  
  171. /*
  172.  * Initialize for processing a scan.
  173.  */
  174.  
  175. METHODDEF void
  176. extract_init (compress_info_ptr cinfo)
  177. {
  178.   /* no work for now */
  179. #ifdef DCT_ERR_STATS
  180.   dcterrorsum = dcterrormax = dctcoefcount = 0;
  181. #endif
  182. }
  183.  
  184.  
  185. /*
  186.  * Clean up after a scan.
  187.  */
  188.  
  189. METHODDEF void
  190. extract_term (compress_info_ptr cinfo)
  191. {
  192.   /* no work for now */
  193. #ifdef DCT_ERR_STATS
  194.   TRACEMS3(cinfo->emethods, 0, "DCT roundoff errors = %d/%d,  max = %d",
  195.        dcterrorsum, dctcoefcount, dcterrormax);
  196. #endif
  197. }
  198.  
  199.  
  200.  
  201. /*
  202.  * The method selection routine for MCU extraction.
  203.  */
  204.  
  205. GLOBAL void
  206. jselcmcu (compress_info_ptr cinfo)
  207. {
  208.   /* just one implementation for now */
  209.   cinfo->methods->extract_init = extract_init;
  210.   cinfo->methods->extract_MCUs = extract_MCUs;
  211.   cinfo->methods->extract_term = extract_term;
  212. }
  213.